package com.servlet;

import com.beans.AdminInfo;
import com.beans.MenuInfo;
import com.beans.PageInfo;
import com.beans.RoleInfo;
import com.dao.AdminDao;
import com.dao.MenuDao;
import com.dao.RoleDao;
import com.jdbc.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/RoleServlet")
public class RoleServlet extends HttpServlet {
    private RoleDao roleDao=new RoleDao();
    private MenuDao menuDao=new MenuDao();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String flag=request.getParameter("flag");
        if("manager".equals(flag)) {
            manager(request,response);
        }
        else if("roleMenuList".equals(flag)) {
            roleMenuList(request,response);
        }
        else if("updateRoleMenu".equals(flag)) {
            updateRoleMenu(request,response);
        }else if("checkreg".equals(flag)){
            checkreg(request,response);
        }else if ("add".equals(flag)){
            add(request,response);
        }else if("updateroleinfo".equals(flag)){
            updateroleinfo(request,response);
        }else if("update".equals(flag)){
            update(request,response);
        }else if ("delete".equals(flag)){
            delete(request,response);
        }else if ("listadmin".equals(flag)){
            listadmin(request,response);
        }else if("shouquan".equals(flag)){
            shouquan(request,response);
        }else if ("updatejuese".equals(flag)){
            updatejuese(request,response);
        }
    }

    private void updatejuese(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String rolename=request.getParameter("rolename");
        int id = Integer.parseInt(request.getParameter("roleId"));
        int n=roleDao.update(rolename,id);
        if(n>0){
            listadmin(request,response);
        }else {
            request.setAttribute("msg","修改失败");
            request.getRequestDispatcher("perm/role-updateadmin.jsp").forward(request, response);
        }

    }

    private void shouquan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        AdminInfo admin=AdminDao.getAdminNamebyId(id);
        List<RoleInfo>roleList=roleDao.getRoleList();
        String rolename=request.getParameter("roleName");
        String pageIndex=request.getParameter("pageIndex");
        request.setAttribute("admin", admin);
        request.setAttribute("roleList", roleList);
        request.setAttribute("rolename",rolename);
        request.setAttribute("pageIndex",pageIndex);
        request.getRequestDispatcher("perm/role-updateadmin.jsp").forward(request, response);
    }

    private void listadmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageIndex=1;
        int pageSize=6;
        int rowCount=AdminDao.getAdminCount();
        String pageIndexStr=request.getParameter("pageIndex");
        if(pageIndexStr!=null) {
            pageIndex=Integer.parseInt(pageIndexStr);
        }PageInfo page= PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
        List<AdminInfo> adminList=roleDao.getAdminList(page);
        request.setAttribute("adminList", adminList);
        request.setAttribute("page", page);
        request.getRequestDispatcher("perm/role-listadmin.jsp").forward(request, response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        roleDao.delete(id);
        manager(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String rolename=request.getParameter("rolename");
        String des=request.getParameter("des");
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId(id);
        roleInfo.setRoleName(rolename);
        roleInfo.setDes(des);
        int n=roleDao.updaterole(roleInfo);
        if(n>0) {
            request.setAttribute("id", id);
            request.setAttribute("adminName", rolename);
            request.setAttribute("note", des);
            request.getRequestDispatcher("perm/role-updateroleinfo.jsp").forward(request, response);
        }
    }


    private void updateroleinfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String rolename=request.getParameter("rolename");
        String des=request.getParameter("des");
        request.setAttribute("id", id);
        request.setAttribute("adminName", rolename);
        request.setAttribute("note", des);
        request.getRequestDispatcher("perm/role-updateroleinfo.jsp").forward(request, response);
    }


    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String rolename=request.getParameter("rolename");
        String des=request.getParameter("des");
        RoleInfo admin=new RoleInfo();
        admin.setRoleName(rolename);
        admin.setDes(des);
        int result=roleDao.addroleinfo(admin);
        if(result!=0) {
            request.setAttribute("msg", "用户添加成功");
            request.getRequestDispatcher("perm/role-add.jsp").forward(request, response);
        }
    }

    private void checkreg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name= (String)request.getParameter("username");
        RoleInfo roleInfo= roleDao.getRoleInfoByName(name);
        if(roleInfo==null)
            response.getWriter().print(0);
        else
            response.getWriter().print(1);

    }


    private void updateRoleMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String [] menuIdList =request.getParameterValues("ids");
        String  [] id  =request.getParameterValues("id");
        int roleId=Integer.parseInt(request.getParameter("roleId"));
        roleDao.updateRoleMenu(roleId,menuIdList,id);
        request.setAttribute("msg", "角色授权成功");
        roleMenuList(request,response);
    }

    private void roleMenuList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<MenuInfo> menuList=menuDao.getMenuList(0);
        int roleId=Integer.parseInt(request.getParameter("roleId"));
        RoleInfo roleInfo =roleDao.getRoleInfoById(roleId);
        String menuIdStr =roleDao.getMenuIdStr(roleId);
        request.setAttribute("menuList", menuList);
        request.setAttribute("roleInfo", roleInfo);
        request.setAttribute("menuIdStr", menuIdStr);
        request.getRequestDispatcher("/perm/role-menu.jsp").forward(request, response);
    }

    private void manager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<RoleInfo> roleList=roleDao.getRoleList();
        request.setAttribute("roleList", roleList);
        request.getRequestDispatcher("/perm/role-manager.jsp").forward(request, response);
    }
}
